import { API } from './BaseApi'
import Utils from '../../vue2-flow/src/utils/Common'
import { JSONParse, JSONStringify } from "@/utils";
import { Project } from "@/api/ProjectsApi";
import { UserComponent } from "@/api/UserComponentsApi";

class UserPageApi {

  // AddUserPageProject (callback) {
  //   API.UserPageProjectApi.AddProject().then(res => {
  //     callback(JSONParse(res))
  //   })
  // }

  GetUserPageProjects (callback) {
    API.UserPageProjectApi.SearchProject().then(res => {
      callback(JSONParse(res))
    })
  }

  /**
   * 根据用户 ID 获取用户页面配置信息
   * @param Id
   * @param callback
   * @constructor
   */
  GetUserPageProjectById (Id, callback) {
    Project.Get(Id).then(res => {
      callback(res.Data)
    })
  }

  /**
   * 修改项目
   * @param Project
   * @param callback
   * @constructor
   */
  UpdateProject (Project, callback) {
    API.UserPageProjectApi.UpdateProject(JSONStringify(Project)).then(res => {
      callback(res)
    })
  }

  /**
   * 复制项目
   * @param ProjectId
   * @param callback
   * @constructor
   */
  CopyProject (ProjectId, callback) {
    Project.CopyProject(ProjectId).then(res=>{
      callback(res)
    })
  }

  /**
   * 保存项目
   * @param ProjectId 项目id
   * @param Page  项目页面
   * @param Component 项目内容/组件
   * @param callback 回调函数
   * @returns {Promise<void>}
   * @constructor
   */
  async SaveUserPageComponents (ProjectId, Page, Component, callback) {
    UserComponent.Save(ProjectId, Page, Component).then(res => {
      // Utils.SetUserPageComponentCache(Page, Component)
      callback(res)
    })
  }

  async GetUserPageComponents (Type, callback) {
    const res = Utils.GetUserPageComponentCache(Type)
    if (!Utils.IsEmpty(res)) {
      return callback(JSONParse(res))
    }
    UserComponent.Gets(Type).then(res => {
      callback(JSONParse(res))
    })
  }
}

export default UserPageApi
